package com.atao.baokede.AiTools;

import cn.hutool.core.io.FileUtil;
import com.atao.baokede.constant.AiConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 * 文件操作工具
 */
public class FileOperationTool {
    private final String FILE_DIR = AiConstant.AI_FILE_ROOT_PATH + "/file";

    @Tool(description = """
            Reads the content of a file from the specified file name.
            This tool is useful when you need to retrieve the contents of a text file for analysis, display, or processing. 
            The file is expected to be located in the directory defined by 'FILE_DIR'. 
            If the file does not exist or cannot be read, an error message will be returned. 
            Invoke this tool when the user asks questions like: 
            'What is the content of the file named [fileName]?', 
            'Can you show me the data in the file [fileName]?', 
            or any similar queries related to reading file content.""")
    public String readFile(@ToolParam(description = """
            The name of the file to read.
            It should be a valid file name without path information (the file is assumed to be in the default directory). 
            Example: 'example.txt'. 
            Note: Ensure that the file exists and is accessible.""")
                           String fileName) {
        String filePath = FILE_DIR + "/" + fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (Exception e) {
            return "Error reading file : " + e.getMessage();
        }
    }

    @Tool(description = """
            Writes the provided content to a file with the specified name.
            This tool is useful when you need to save data or text into a file for storage or further use.
            The file will be created or overwritten in the directory defined by 'FILE_DIR'.
            If the operation fails due to permission issues or other errors, an error message will be returned.
            Invoke this tool when the user asks questions like:
            'Can you save the following content to a file named [fileName]?',
            'Write this data into a file called [fileName].',
            or any similar queries related to writing content to a file.""")
    public String writeFile(@ToolParam(description = """
            The name of the file to write.
            It should be a valid file name without path information (the file will be created in the default directory). 
            Example: 'output.txt'. 
            Note: Ensure that you have write permissions for the target directory.""")
                            String fileName,
                            @ToolParam(description = """
            The content to write into the file.
            It can be plain text, JSON, or any string data.
            Example: 'This is the content to be saved.'.
            Note: Large content may impact performance or cause memory issues.""")
                            String content) {
        String filePath = FILE_DIR + "/" + fileName;
        try {
            FileUtil.writeUtf8String(content, filePath);
            return "File written successfully to : " + filePath;
        } catch (Exception e) {
            return "Error writing file : " + e.getMessage();
        }
    }
}
